package com.twu.biblioteca.category;

import com.twu.biblioteca.library.Catalogue;
import com.twu.biblioteca.user.User;
import com.twu.biblioteca.utils.IO;

/**
 * Created by Enrico on 17/09/2014.
 */
public class CategoryManager {

    private Category category;

    public CategoryManager(){}

    public CategoryManager(Catalogue catalogue){
        category = new Category(catalogue);
    }

    public void setup(User user){
        user.sayHello();

        char choice = validChoice();
        while (!isExit(choice)) {
            menu(choice, user);
            choice = validChoice();
        }
        System.exit(0);
    }

    public char validChoice() {
        char choice = readChoice();
        while (!valid(choice)) {
            explainError();
            choice = readChoice();
        }
        return choice;
    }

    public char readChoice() {
        System.out.println();
        System.out.print("Books, Movies, User Information or Exit (b/m/i/x): ");
        return Character.toLowerCase(IO.nextChar());
    }

    public boolean valid(char choice) {
        String choices = "bmix";
        return choices.contains(Character.toString(choice));
    }

    public void menu(char choice, User user) {
        switch (choice) {
            case 'b':
                category.books(user);
                break;
            case 'm':
                category.movies(user);
                break;
            case 'i':
                user.showInfo();
                break;
        }
    }

    private void explainError() {
        System.out.println("Select a valid option!");
    }

    public boolean isExit(char choice) {
        return choice == 'x';
    }
}
